package com.holyn.guofang.view.Login;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.login.GetPasswordContract;
import com.holyn.guofang.presenter.login.GetPasswordPresent;
import com.holyn.guofang.utils.ValidUtil;
import com.holyn.guofang.view.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/6/12.
 */

public class GetPasswordNextActivity extends BaseActivity implements BaseView, GetPasswordContract.View {

    private static final int REQUEST_ACTION_FIND_PASSWORD = 0x0001;

    @BindView(R.id.et_validCode)
    EditText etValidCode;
    @BindView(R.id.et_password_new)
    EditText etPasswordNew;
    @BindView(R.id.et_password_repeat)
    EditText etPasswordRepeat;
    @BindView(R.id.btn_find_password)
    Button btnFindPassword;

    private GetPasswordContract.Presenter getPasswordPresent;

    private Integer checkMethod;
    private String equipmentCode;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_get_password_next;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("密码找回");

        getPasswordPresent = new GetPasswordPresent(this);
        getPasswordPresent.addViewListener(this);

        checkMethod = getIntent().getIntExtra("checkMethod", 1);
        equipmentCode = getIntent().getStringExtra("equipmentCode");

        initView();

    }

    private void initView() {

    }

    @OnClick(R.id.btn_find_password)
    public void onViewClicked() {
        String validCode = etValidCode.getText().toString();
        if (TextUtils.isEmpty(validCode)) {
            showToastDialog("请输入验证码");
            return;
        }
        String password = etPasswordNew.getText().toString();
        if (!ValidUtil.isContainWord(password) || !ValidUtil.isGreaterMinLength(8, password)) {
            showToastDialog("密码要求最少8位的字母与数字组合");
            return;
        }
        String passwordRepeat = etPasswordRepeat.getText().toString();
        if (!password.equals(passwordRepeat)) {
            showToastDialog("两次输入的密码不相同");
            return;
        }

        getPasswordPresent.postChangePasswordCode(checkMethod, equipmentCode, validCode, password, REQUEST_ACTION_FIND_PASSWORD);

    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, StatusWo statusWo) {

        if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS) {
            toastMsg("密码重置成功");
            finish();
        } else if (statusWo.getStatus() == HttpConfigure.STATUS_ERROR) {
            toastMsg("密码重置失败");
        } else if (statusWo.getStatus() == -1) {
            toastMsg("手机号码或电子邮箱不存在");
        } else if (statusWo.getStatus() == -2) {
            toastMsg("手机或电子邮箱收到的验证码不匹配");
        } else if (statusWo.getStatus() == -3) {
            toastMsg("验证码不匹配");
        }
    }
}
